package cc.smarnet.caihou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cc.smarnet.caihou.R;
import cc.smarnet.caihou.entity.Deliver;


/**
 * 出库信息的adapter
 */
public class DeliverAdapter2 extends RecyclerView.Adapter implements View.OnClickListener{
    private Context context;
    private List<Deliver> list;

    public DeliverAdapter2(Context context, List<Deliver> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.deliver_item2, parent, false);
        deliverViewHolder deliverViewHolder = new deliverViewHolder(view);
        return deliverViewHolder;
    }
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder  holder, int position) {
        Deliver deliver = list.get(position);

        deliverViewHolder deliverViewHolder = (deliverViewHolder) holder;
//        deliverViewHolder.carDerver.setText(deliver.getCarDerver());
//        deliverViewHolder.carNo.setText(deliver.getCarNo());
        deliverViewHolder.number.setText(deliver.getNumber());
        deliverViewHolder.orderNo.setText(deliver.getOrderNo());
//        deliverViewHolder.state.setText(deliver.getStateSting());
//        deliverViewHolder.stockInTime.setText(deliver.getStockInTime());
//        deliverViewHolder.stockOutUser.setText(deliver.getStockOutUser());
        deliverViewHolder.user.setText(deliver.getUser());
        deliverViewHolder.touser.setText(deliver.getTouser());
        deliverViewHolder.phone.setText(deliver.getPhone());
        deliverViewHolder.remarks.setText(deliver.getRemarks());

        holder.itemView.setTag(position);
        deliverViewHolder.custom_print.setTag(position);
    }

    @Override
    public int getItemCount() {
        // list.size()为0的话也必须至少返回1，返回有数值的话才会显示item，因为该方法不会返回0
        return list.size() > 0 ? list.size() : 1;
    }

    class deliverViewHolder extends RecyclerView.ViewHolder {
        TextView carDerver;
        TextView carNo;
        TextView number;
        TextView orderNo;
        TextView state;
        TextView stockInTime;
        TextView stockOutUser;
        TextView user;
        TextView touser;
        TextView phone;
        TextView remarks;
        private Button custom_print;


        public deliverViewHolder(@NonNull View itemView) {
            super(itemView);
//            carDerver = itemView.findViewById(R.id.deliver_item_carDerver);
//            carNo = itemView.findViewById(R.id.deliver_item_carNo);
            number = itemView.findViewById(R.id.deliver_item_number);
            orderNo = itemView.findViewById(R.id.deliver_item_orderNo);
//            state = itemView.findViewById(R.id.deliver_item_state);
//            stockInTime = itemView.findViewById(R.id.deliver_item_stockInTime);
//            stockOutUser = itemView.findViewById(R.id.deliver_item_stockOutUser);
            user = itemView.findViewById(R.id.user);
            touser = itemView.findViewById(R.id.touser);
            phone = itemView.findViewById(R.id.phone);
            remarks = itemView.findViewById(R.id.deliver_item_remarks);

            custom_print = itemView.findViewById(R.id.custom_print);

            // item点击事件
//            itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Deliver deliver = list.get(getAdapterPosition());
//                    Intent intent = new Intent(context, DeliverDetailActivity.class);
//                    intent.putExtra("deliverDetail", (Serializable) deliver);
//                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    context.startActivity(intent);
//                }
//            });

            // 为ItemView添加点击事件
            itemView.setOnClickListener(DeliverAdapter2.this);
            custom_print.setOnClickListener(DeliverAdapter2.this);
        }
    }
    //=======================以下为item中的button控件点击事件处理===================================

    //item里面有多个控件可以点击
    public enum ViewName {
        ITEM,
        PRACTISE
    }

    //自定义一个回调接口来实现Click和LongClick事件
    public interface OnItemClickListener {
        void onItemClick(View v, ViewName viewName, int position);

        void onItemLongClick(View v);
    }

    private OnItemClickListener mOnItemClickListener;//声明自定义的接口

    //定义方法并暴露给外面的调用者
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public void onClick(View v) {

        int position = (int) v.getTag(); //getTag()获取数据
        if (mOnItemClickListener != null) {
            switch (v.getId()) {
                case R.id.custom_print://
                    mOnItemClickListener.onItemClick(v, ViewName.PRACTISE, position);
                    break;
                default:
                    mOnItemClickListener.onItemClick(v, ViewName.ITEM, position);
                    break;
            }
        }
    }
}
